package com.eleadmin.order.service.impl;

import com.eleadmin.common.core.config.ConfigProperties;
import com.eleadmin.damon.utils.results.PageResult2;
import com.eleadmin.common.core.web.JsonResult;
import com.eleadmin.common.system.entity.User;
import com.eleadmin.order.biz.CustomerBiz33;
import com.eleadmin.order.biz.OceanFeeBiz;
import com.eleadmin.order.entity.Customer23123;
import com.eleadmin.order.entity.Ocean;
import com.eleadmin.order.entity.OceanFee;
import com.eleadmin.order.service.OceanService;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.stereotype.Service;
import com.eleadmin.order.biz.OceanBiz;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.OutputStream;
import java.util.List;

/**
 * 海运服务实现类
 * @author : Doman
 * @date : 2023-04-24 11:26:09
 * @version ：1.0
 */
@Service
public class OceanServiceImpl /*extends ServiceImpl<OceanMapper, Ocean> */implements OceanService {

    @Autowired
    private OceanBiz oceanBiz;
    @Autowired
    private OceanFeeBiz oceanFeeBiz;
    @Autowired
    private CustomerBiz33 customerBiz;

    @Override
    public JsonResult list(PageResult2<T> page, Ocean ocean, User loginUser)  throws Exception{
        return oceanBiz.list(page,ocean,loginUser);
    }

    @Override
    public JsonResult list(Ocean ocean, User loginUser) {
        return JsonResult.ok().setData(oceanBiz.list(ocean,loginUser));
    }

    @Override
    public JsonResult getById(String id) {
        return JsonResult.ok().setData(oceanBiz.getById(id));
    }

    @Transactional
    @Override
    public JsonResult save(Ocean ocean, User loginUser) {
        Customer23123 customer = customerBiz.getById(ocean.getCustomerId()+"");
        ocean.setSalesmanid(customer.getSalesmanid());
        ocean.setSalesman(customer.getSalesman());
        ocean.setOperaterid(customer.getOperaterid());
        ocean.setOperater(customer.getOperater());
        if (oceanBiz.save(ocean, loginUser)) {
            return JsonResult.ok("添加成功");
        }
        return JsonResult.error("添加失败");
    }

    @Transactional
    @Override
    public JsonResult update(Ocean ocean, User loginUser) {
        if (oceanBiz.update(ocean, loginUser)) {
            return JsonResult.ok("修改成功");
        }
        return JsonResult.error("修改失败");
    }

    @Transactional
    @Override
    public JsonResult remove(String id, User loginUser) {
        if (oceanBiz.remove(id, loginUser)) {
            return JsonResult.ok("删除成功");
        }
        return JsonResult.error("删除失败");
    }





    //导出客户海运账单
    @Transactional
    @Override
    public void exportsOcean(Ocean ocean, User loginUser, HttpServletResponse response, HttpServletRequest request) throws Exception{
        Ocean ocean2 = oceanBiz.getById(ocean.getId());
        OceanFee oceanFee = new OceanFee();
        oceanFee.setOceanId(ocean2.getId());
        oceanFee.setType("应收");
        List<OceanFee> oceanFees = oceanFeeBiz.list(oceanFee, loginUser);

        // 根据模板创建excel工作簿
        XSSFWorkbook xssfWorkbook = new XSSFWorkbook(getUploadDir() + "ocean_new.xlsx");

        // 获取创建的工作簿第一页
        XSSFSheet sheet = xssfWorkbook.getSheet("Sheet1");
        sheet.setForceFormulaRecalculation(true);//公式不自动执行的问题

        //创建列，即单元格Cell
        XSSFRow xssfRow =  null;
        //创建列，即单元格Cell
        XSSFCell xssfCell = null;

        //把Excel模板文件里的数据清空
        setBlankExcel(xssfCell, xssfRow, sheet);
        // 获取第一行
        xssfRow = sheet.getRow(4);


        // 先定义创建excel表头
        xssfCell = xssfRow.getCell(0); //创建单元格
   /* Font fontStyle = xssfWorkbook.createFont(); // 字体样式
    fontStyle.setBold(true); // 加粗
    fontStyle.setFontName("华文楷体"); // 字体
    fontStyle.setFontHeightInPoints((short) 13.5); // 大小
//    fontStyle.setFontHeight((short)1500);
    XSSFCellStyle cellStyle = xssfWorkbook.createCellStyle(); // 单元格样式
    cellStyle.setAlignment(HorizontalAlignment.LEFT);
    // 将字体样式添加到单元格样式中
    cellStyle.setFont(fontStyle);
    xssfCell.setCellStyle(cellStyle);*/
        xssfCell.setCellValue("To:" + ocean2.getCustomer());

        xssfRow = sheet.getRow(6);
        xssfCell = xssfRow.getCell(0); //创建单元格
        xssfCell.setCellValue("提单号:" + ocean2.getBillOfLading());
        xssfRow = sheet.getRow(7);
        xssfCell = xssfRow.getCell(0); //创建单元格
        xssfCell.setCellValue("船名航次:" + ocean2.getVessel()+"/ "+ocean2.getVoyage());
        xssfRow = sheet.getRow(8);
        xssfCell = xssfRow.getCell(0); //创建单元格
        xssfCell.setCellValue("柜型:" + ocean2.getBoxType());

        OceanFee oceanFee1 = null;
        float sum = 0, usd = 0, cny = 0;
        if (oceanFees != null && oceanFees.size() > 0) {
            for (int i = 0; i < oceanFees.size(); i ++) {
                OceanFee oceanFee2 = oceanFees.get(i);
                xssfRow = sheet.getRow(11+i);
                xssfCell = xssfRow.getCell(0); //创建单元格
                xssfCell.setCellValue(oceanFee2.getFeeName());
                xssfCell = xssfRow.getCell(1); //创建单元格
                xssfCell.setCellValue(oceanFee2.getPrice());
                xssfCell = xssfRow.getCell(2); //创建单元格
                xssfCell.setCellValue(1);
                try {
                    if (oceanFee2.getCurrency().equals("CNY")) {
                        xssfCell = xssfRow.getCell(4); //创建单元格
                        xssfCell.setCellValue(oceanFee2.getPrice());
                        cny += Integer.valueOf(oceanFee2.getPrice());
                    } else {
                        xssfCell = xssfRow.getCell(3); //创建单元格
                        xssfCell.setCellValue(oceanFee2.getPrice());
                        usd += Integer.valueOf(oceanFee2.getPrice());
                    }
                    sum += Integer.valueOf(oceanFee2.getPrice());
                } catch (Exception e){

                }
                xssfCell = xssfRow.getCell(5); //创建单元格
                xssfCell.setCellValue(oceanFee2.getRemark());
            }
        }

//        xssfRow = sheet.getRow(27);
//        xssfCell = xssfRow.getCell(0); //创建单元格
//        xssfCell.setCellValue("合计:"+ sum);
        xssfRow = sheet.getRow(28);
        xssfCell = xssfRow.getCell(0); //创建单元格
        xssfCell.setCellValue("应收USD:"+ usd);
        xssfCell = xssfRow.getCell(5); //创建单元格
        xssfCell.setCellValue("应收RMB:"+ cny);

        try {

            // 创建Excel文件的输入流对象
            OutputStream os = response.getOutputStream();
            // 这时候把创建好的excel写入到输出流
            xssfWorkbook.write(os);
            // 关闭流
            os.flush();
            os.close();
        } catch (Exception e) {
            e.printStackTrace();
            response.setContentType("text/html; charset=UTF-8");
            response.getWriter().println("没有对应的数据哦,请再次查询!");
        }
    }

    public void setBlankExcel(XSSFCell xssfCell, XSSFRow xssfRow, XSSFSheet sheet){
        xssfRow = sheet.getRow(6);
        xssfCell = xssfRow.getCell(0); //创建单元格
        xssfCell.setCellValue("" );
        xssfRow = sheet.getRow(7);
        xssfCell = xssfRow.getCell(0); //创建单元格
        xssfCell.setCellValue("" );
        xssfRow = sheet.getRow(8);
        xssfCell = xssfRow.getCell(0); //创建单元格
        xssfCell.setCellValue("" );
        for (int i = 11; i < 28; i++) {
            xssfRow = sheet.getRow(i);
            xssfCell = xssfRow.getCell(0); //创建单元格
            xssfCell.setCellValue("" );
            xssfCell = xssfRow.getCell(1); //创建单元格
            xssfCell.setCellValue("" );
            xssfCell = xssfRow.getCell(2); //创建单元格
            xssfCell.setCellValue("" );
            xssfCell = xssfRow.getCell(3); //创建单元格
            xssfCell.setCellValue("" );
            xssfCell = xssfRow.getCell(4); //创建单元格
            xssfCell.setCellValue("" );
            xssfCell = xssfRow.getCell(5); //创建单元格
            xssfCell.setCellValue("" );
        }

        xssfRow = sheet.getRow(28);
        xssfCell = xssfRow.getCell(0); //创建单元格
        xssfCell.setCellValue("" );
        xssfCell = xssfRow.getCell(5); //创建单元格
        xssfCell.setCellValue("" );
    }

    @Resource
    private ConfigProperties config;

    /**
     * 文件上传基目录
     */
    private String getUploadBaseDir() {
        return File.listRoots()[config.getUploadLocation()].getAbsolutePath()
                .replace("\\", "/") + "/upload/";
    }

    /**
     * 文件上传位置
     */
    private String getUploadDir() {
        return getUploadBaseDir() + "file/";
    }
}
